/*
 * @Author: szx
 * @Date: 2022-04-22 14:48:34
 * @LastEditTime: 2022-04-22 14:50:58
 * @Description:
 * @FilePath: \leetcode\100-199\131\131_3.js
 */
/**
 * @param {string} s
 * @return {string[][]}
 */
var partition = function (s) {
    const len = s.length,
        pre = new Array(len).fill(0).map(() => new Array(len).fill(true));
    for (let i = len - 1; i >= 0; i--) {
        for (let j = i + 1; j < len; ++j) {
            pre[i][j] = s[i] == s[j] && pre[i + 1][j - 1];
        }
    }
    const res = [];
    const backtrack = (path, index) => {
        if (index >= len) {
            res.push(Array.from(path));
            return;
        }
        for (let i = index; i < len; i++) {
            if (!pre[index][i]) continue;
            path.push(s.substring(index, i + 1));
            backtrack(path, i + 1);
            path.pop();
        }
    };
    backtrack([], 0);
    return res;
};
